package com.order.controller.back_end;

import com.order.common.AuthAccess;
import com.order.dao.Dish;
import com.order.dao.DishType;
import com.order.dao.Result;
import com.order.service.DishTypeservice;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Scope("prototype")
@AllArgsConstructor
@CrossOrigin
public class DishTypeController {

    private DishTypeservice dishTypeservice;

//    新增菜系
@RequestMapping("/dish_typeAdd")
    public  Result add(@RequestBody DishType dishType, String name){

            try{
            dishTypeservice.addDishType(dishType);
        } catch (Exception e){
            if(e instanceof DuplicateKeyException){
                return Result.error("插入数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        log.info("响应成功");

        return Result.success();

/*    int count = dishTypeservice.judgment(name);
    if(count > 0){
//        判断查询
        log.info("该菜系已存在，不可增加！");
        return "新增失败";
    }else{
        int result = dishTypeservice.addDishType(dishType);
        String dishTypeName= dishType.getName();
        if(result > 0){
            log.info("新增"+ dishTypeName +"成功！");
            return "新增成功";
        }else {
            log.info("新增失败");
            return "新增失败";
        }
    }*/

  }


//    删除菜系，修改del为 1
    @RequestMapping("/dish_typeDelete/{id}")
    public  String delete(@PathVariable("id") int id){
        int count = dishTypeservice.deleteDishType(id);
        if( count>0 ){
            log.info("删除"+ id +"号成功");
            return "删除成功";
        }else {
            return "删除失败";
        }
    }


//    修改菜系
    @RequestMapping("/dish_typeUpdate/{id}")
    public String update(@PathVariable("id")int id, DishType dishType){
        int count = dishTypeservice.changeDishType(dishType);
        String dishTypeName= dishType.getName();
        if( count>0 ){
            log.info("已成功修改为："+ dishTypeName);
            return "修改成功";
        }else {
            return "修改失败";
        }

    }


//    查询菜系
    @RequestMapping("/dish_typeSelect")
    public Result query(DishType dishType) {
        List<DishType> dishTypes;
        try{
             dishTypes = dishTypeservice.selectDishType(dishType);
        } catch (Exception e){
            if(e instanceof DuplicateKeyException){
                return Result.error("查询数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        log.info("响应成功");

        return Result.success(dishTypes);

    }




}